package ch.sv7.tool.dbmanager.db.fix;

import ch.sv7.tool.dbmanager.db.ColumnSchema;
import ch.sv7.tool.dbmanager.db.SchemaElement;
import ch.sv7.tool.dbmanager.db.dialect.DialectException;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class ImplicitUpdateColumnFix extends AbstractSchemaFix implements SchemaFix {


	private ColumnSchema columnSchema;
	private boolean ignoreDefault;
	private boolean fixNullValues;
	
	/**
	 * @param missingColumnDifference
	 */
	public ImplicitUpdateColumnFix(ColumnSchema columnSchema) {
		super();
		this.columnSchema = columnSchema;
	}
	
	public boolean isIgnoreDefault() {
		return ignoreDefault;
	}

	public void setIgnoreDefault(boolean ignoreDefault) {
		this.ignoreDefault = ignoreDefault;
	}

	public boolean isFixNullValues() {
		return fixNullValues;
	}

	public void setFixNullValues(boolean fixNullValues) {
		this.fixNullValues = fixNullValues;
	}

	public SchemaElement getActionTarget() {
		return columnSchema;
	}
	
	public String getSQLFix() throws DialectException {
		String ddl = "\n-- Update column Fix \n";
		if (fixNullValues){
			ddl += getDialect().getFixNullValuesSQL(columnSchema);
		}
		ddl += getDialect().getUpdateColumnDDL(columnSchema, ignoreDefault);
		return ddl;
	}

	public int getPriority() {
		return SchemaFix.PRIORITY_COLUMN_IMPLICIT_UPDATE;
	}

	public String getDescription() {
		return "Implicit Update column " + columnSchema.getTable().getName() + "." + columnSchema.getName() + " from table " + columnSchema.getTable().getName(); 
	}

	public int getAction() {
		return ACTION_EDIT;
	}

	
}
